#!/usr/bin/perl -w

### include the file with HTML processing heavy lifting
require("processHTMLDocument.pl");

sub parseHTMLDocument
{
	## get document path from args
	my $htmlfile = shift;
	my $outpath = shift;
	my $outfile = shift;
	
	#print "htmlfile: $htmlfile\n";
	#print "outpath: $outpath\n";
	$outfile =~ s/\.html$/\.txt/i;
	#print "outfile: $outfile\n";
	
	my $txtfile = $outpath.$outfile;
	
	if(-e $txtfile)
	{
		return;
	}
	elsif(-z $txtfile)
	{
		print "File exists but is empty:  Replacing it...\n";
	}
	
	my $OUTF;
	if(!open($OUTF, ">".$txtfile))
	{
		print "Unable to open file $txtfile for output!\n";
		return;
	}

	print "Processing \"$txtfile\"... ";
	
	
	my $s = processHTMLDocument($htmlfile, $OUTF);
	if($s)
	{
		## try to open the output file first
		#if(open(my $w, ">".$txtfile))
		#{
		#	## TODO:  Write output file
		#}
		#else
		#{
		#	print "Unable to open file $w for output!\n";
		#	return;
		#}
		print "success\n";
	}
	else
	{
		print "\n$htmlfile processing failed (perhaps this patent was withdrawn)\n";
	}
	
	close $OUTF;
}

## ensures that all directories in the path exist
sub initPathTrace
{
	## get the path trace array
	my @trace = @_;
	my $path = "";
	
	foreach $p (@trace)
	{
		## add the / last so that first time through is valid
		$path = $path . $p . "/";
		if(!mkdir($path))
		{
			#print "Couldn't make directory $path\n";
			#return;
		}
	}
}

sub makePath
{
	my @trace = @_;
	my $path = "";
	
	foreach $p (@trace)
	{
		## add the / last so that first time through is valid
		$path = $path . $p . "/";
	}
	
	return $path;
}

## enter into a directory searching for HTML files to parse or other directories
sub recurseDirectory 
{
	## get local args
	my $folder = shift;
	my $startRange = shift;
	my $endRange = shift;
	my @trace = @_;
	
	## list all items in this directory
	opendir(my $DIR, $folder);
	my @dirs = readdir($DIR);
	closedir($DIR);
	
	## don't ask why I'm using an indexed for-loop here
	## scan the directory for more directories and html files and process accordingly
	REC: for(my $i = 0; $i < scalar(@dirs); $i++)
	{
		my $d = $dirs[$i];
		
		if(($d ne ".") && ($d ne ".."))
		{
			my $file = "$folder/$d";
			
			if (-d $file)
			{	
				print "Entering Directory \"$file\"\n";
				
				push(@trace, $d);
				recurseDirectory($file, $startRange, $endRange, @trace);
				pop(@trace);
			}
			elsif((-f $file) && ($d =~ /^(\d+)\.html$/i))
			{
				my $rval = $1;
				## see if we are using ranges, and if so check against them
				if($startRange > 0)
				{
					#print "START RANGE VIOLATION:\n1: $1\n";
					next REC if($rval < $startRange);
				}
				if($endRange > 0)
				{
					#print "END RANGE VIOLATION:\n1: $1\n";
					next REC if($rval > $endRange);
				}
				
				## ensure that the output path exists
				initPathTrace(@trace);
				## process this html document
				parseHTMLDocument($file, makePath(@trace), $d);
			}
		}
	}
}


my $range = -1;
my $folder = "html";
my $outdir = "txt";

## parse command line arguments, check for initial directory/range
#my $initdir = shift;
my $startRange = shift;
my $endRange = shift;

print "start range:  $startRange\n";
print "end range: $endRange\n";	

#if($initdir)
#{
#	$folder = $initdir;
#}

## create the output directory if it doesn't already exist
## TODO:  Clean this up and add more checks
if(!(-e $outdir))
{
	mkdir $outdir; 
}
my @trace;
push(@trace, $outdir);

if(!$startRange)
{
	$startRange = -1;
}
if(!$endRange)
{
	$endRange = -1;
}

## ensure that STDOUT autoflushes so that we can see status outputs in real time
$| = 1;
recurseDirectory($folder, $startRange, $endRange, @trace);
